package org.autocode.util;

import org.autocode.code.ColumnInfo;
import org.autocode.code.TableInfo;
import org.autocode.config.DatasourceConfig;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: autocode
 * @description:
 * @author: zhu.xiaojie
 * @create: 2020-08-16 17:21
 */
public class JdbcHelper {

    public static List<String> listDatabase(DatasourceConfig config) throws SQLException {
        try(Connection connection = createConnection(config)){
            String sql = "show databases;";

            List<String> databases = new ArrayList<>();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                String database = resultSet.getString(1);
                databases.add(database);
            }

            return databases;
        }catch (Exception e){
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    private static Connection createConnection(DatasourceConfig config) throws SQLException {
        String database = "mysql";
        return createConnection(config , database);
    }
    public static Connection createConnection(DatasourceConfig config , String database) throws SQLException {

        String url = "jdbc:mysql://"+ config.getHost() +":"+ config.getPort() +"/" + database;
        Connection connection = DriverManager.getConnection(url, config.getUsername(), config.getPassword());
        return connection;
    }

    public static List<String> listTable(DatasourceConfig config , String databas){
        try(Connection connection = createConnection(config)){

            Statement statement = connection.createStatement();
            statement.execute("use " + databas + ";");

            ResultSet resultSet = statement.executeQuery("show tables");
            List<String> tables = new ArrayList<>();
            while (resultSet.next()){
                String table = resultSet.getString(1);
                tables.add(table);
            }
            return tables;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return new ArrayList<>();
        }
    }


    public static String showCreateTable(DatasourceConfig config , String database , String table){
        try(Connection connection = createConnection(config , database)){

            String sql = "show create table " + table + ";";

            ResultSet resultSet = connection.createStatement().executeQuery(sql);
            resultSet.next();
            String result = resultSet.getString(2);
            return result;

        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }

    public static TableInfo getTableInfo(DatasourceConfig config , String database , String table){
        try(Connection connection = createConnection(config , database)){
            DatabaseMetaData metaData = connection.getMetaData();

            TableInfo tableInfo = new TableInfo();
            tableInfo.setTableName(table);



            List<ColumnInfo> columnInfos = new ArrayList<>();
            ResultSet columns = metaData.getColumns(null, database, table, "%");
            while (columns.next()){
                String columnName = columns.getString("COLUMN_NAME");
                int type = columns.getInt("DATA_TYPE");
                String columnComment = columns.getString("REMARKS");

                ColumnInfo columnInfo = new ColumnInfo();
                columnInfo.setColumnComment(columnComment);
                columnInfo.setColumnName(columnName);
                columnInfo.setColumnType(type);

                columnInfos.add(columnInfo);
            }

            tableInfo.setColumns(columnInfos);

            String sql = showCreateTable(config, database, table);
            tableInfo.setSql(sql);


            return tableInfo;


        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
